package com.resou.reader.mine.vipcenter.adapter;

import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.resou.reader.R;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.data.bookstore.model.PlateNovels;

import java.util.List;

/**
 * 换一换适配器
 */
public class MonthlyVipGrid_ListAdapter extends BaseMultiItemQuickAdapter<PlateNovels, BaseViewHolder> {
    public MonthlyVipGrid_ListAdapter(@Nullable List<PlateNovels> data) {
        super(data);
        addItemType(0, R.layout.adapter_monthlyvip_bookinfo_grid);
        addItemType(1, R.layout.adapter_monthlyvipheader);
    }

    @Override
    protected void convert(BaseViewHolder helper, PlateNovels item) {
        switch (helper.getItemViewType()) {
            case 0:
                Glide.with(mContext).load(item.getCoverUrl() + "").into((ImageView) helper.getView(R.id.ivBookPic));
                helper.setText(R.id.tvBookName, item.getNovelName())
                        .setText(R.id.tvMoney, item.getCnyPrice() + "");
                TextView price = helper.getView(R.id.tvMoney);
                price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                LinearLayout linearLayout = helper.getView(R.id.llOnclick_Grid);
                linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BookDetailActivity.startDetailActivity(mContext, item.getNovelId());
                    }
                });
                break;
            case 1:
                Glide.with(mContext).load(item.getCoverUrl() + "").into((ImageView) helper.getView(R.id.ivBookPic));
                helper.setText(R.id.tvBookName, item.getNovelName())
                        .setText(R.id.tvContent, item.getNovelDescription() + "")
                        .setText(R.id.tvAuthor, item.getAuthorName() + "")
                        .setText(R.id.tvMoney, item.getCnyPrice() + "");
                TextView price2 = helper.getView(R.id.tvMoney);
                price2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                LinearLayout linearLayout2 = helper.getView(R.id.llBookInfo);
                linearLayout2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BookDetailActivity.startDetailActivity(mContext, item.getNovelId());
                    }
                });
                break;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {   // 布局是GridLayoutManager所管理
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    // 如果是Header、Footer的对象则占据spanCount的位置，否则就只占用1个位置
                    return isHeader(position) ? gridLayoutManager.getSpanCount() : 1;
                }
            });
        }
    }

    /**
     * 判断是否是Header的位置
     * 如果是Header的则返回true否则返回false
     *
     * @param position
     * @return
     */
    public boolean isHeader(int position) {
        return position == 0;
    }
}
